/**
 * 自动为所有HTML文件添加弹窗效果的脚本
 * 
 * 使用方法:
 * 1. 确保安装了Node.js
 * 2. 打开命令行，进入项目根目录
 * 3. 运行 node add-modal-to-all-pages.js
 */

const fs = require('fs');
const path = require('path');

// 待处理的页面列表
const pages = [
    'model-config.html',
    'model-details.html',
    'model-performance.html',
    'model-update.html',
    'quality-analysis.html',
    'online-analysis.html',
    'production-monitor.html',
    'user-management.html',
    'system-settings.html',
    'alert-settings.html',
    'data-management.html',
    'history-data.html',
    'index.html'
];

// 处理每个页面
pages.forEach(page => {
    try {
        // 读取文件内容
        const filePath = path.join(__dirname, page);
        let content = fs.readFileSync(filePath, 'utf8');
        
        // 检查是否已经添加了弹窗样式和脚本
        const hasModalStyle = content.includes('modal-dialog.css');
        const hasModalScript = content.includes('modal-dialog.js');
        const hasCommonScript = content.includes('add-modal-to-pages.js');
        
        // 添加弹窗样式
        if (!hasModalStyle) {
            content = content.replace(
                /<link rel="stylesheet" href="styles\/scale-fix.css">/,
                '<link rel="stylesheet" href="styles/scale-fix.css">\n    <link rel="stylesheet" href="styles/modal-dialog.css">'
            );
        }
        
        // 添加弹窗脚本
        if (!hasModalScript) {
            content = content.replace(
                /<script src="scripts\/unified.js"><\/script>/,
                '<script src="scripts/unified.js"></script>\n    <script src="scripts/modal-dialog.js"></script>'
            );
        }
        
        // 添加通用弹窗处理脚本
        if (!hasCommonScript && hasModalScript) {
            content = content.replace(
                /<script src="scripts\/modal-dialog.js"><\/script>/,
                '<script src="scripts/modal-dialog.js"></script>\n    <script src="scripts/add-modal-to-pages.js"></script>'
            );
        }
        
        // 写入文件
        fs.writeFileSync(filePath, content, 'utf8');
        console.log(`✅ 已更新 ${page}`);
    } catch (error) {
        console.error(`❌ 处理 ${page} 失败:`, error.message);
    }
});

console.log('✨ 所有页面更新完成!'); 